package com.briup.common.config;

import com.briup.common.properties.CorsConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * webmvc的配置类
 */
@ConditionalOnProperty("briup.config.cors.mapping")
@Configuration
public class WebCorsAutoConfig implements WebMvcConfigurer {

	@Autowired
	private CorsConfigProperties corsConfigProperties;


	// 跨域配置
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping(corsConfigProperties.getMapping())
				.allowedOrigins(corsConfigProperties.getAllowedOrigins())
				.allowedMethods(corsConfigProperties.getAllowedMethods())
				.allowedHeaders(corsConfigProperties.getAllowedHeaders())
				.allowCredentials(corsConfigProperties.isAllowCredentials())
				.maxAge(corsConfigProperties.getMaxAge());
//		registry.addMapping("/**")
//				.allowedOrigins("*")
//				.allowedMethods("GET","POST","PUT","OPTIONS","DELETE","PATCH")
//				.allowedHeaders("*")
//				.allowCredentials(false)
//				.maxAge(3600);
	}
}
